package com.bclz.cat.nouniquekey;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

public class CatReducer1 extends Reducer<IntWritable,Text,NullWritable,Text> {


    @Override
    protected void reduce(IntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        Iterator<Text> iterator = values.iterator();
        int sum=0;
        String catName="";
        while(iterator.hasNext()){
            Text next = iterator.next();
            String s = next.toString();
            String[] split = s.split(",");
            if(sum==0){
                catName=split[1];
            }

            int count = Integer.parseInt(split[2]);
            sum+=count;

        }
        context.write(NullWritable.get(),new Text(key.get()+","+catName+","+sum));

    }
}
